<?php
class EditAction extends CAction {

	public function run() {
		$this->controller->layout = 'project';

		if (isset ($_POST['ObjectiveForm'])) {
			$this->saveObjectiveForm($_POST['ObjectiveForm'], true);
		}
		elseif (isset ($_POST['TopicValidationForm'])) {
			$this->saveTopicForm($_POST['TopicValidationForm'], isset ($_POST['next_button']));
		}
		elseif (isset ($_POST['TopicObjectiveForm'])) {
			$this->saveNewObjectiveForm($_POST['TopicObjectiveForm'], isset ($_POST['next_button']));
		}
		elseif (isset($_POST["Meetings"])){
			$this->saveEndOfMeeting($_POST['Meetings'], isset ($_POST['next_button']));
		} else {
			$model = new ObjectiveForm();
			$this->controller->render('edit_1_objectives', array (
				'model' => $model
			));
		}
	}

	protected function saveObjectiveForm($arr, $next) {
		$model = new ObjectiveForm();
		$model->attributes = $arr;
		if (!$model->save()) {
			$this->controller->render('edit_1_objectives', array (
				'model' => $model
			));
		} else {
			$model = new TopicValidationForm();
			$this->controller->render('edit_2_topics', array (
				'model' => $model
			));
		}

	}

	protected function saveTopicForm($arr, $next) {
		$model = new TopicValidationForm();
		$model->attributes = $arr;

		if (!$model->save()) {
			$this->controller->render('edit_2_topics', array (
				'model' => $model
			));
			return;
		} else {
			if ($next) {
				$model = new NewObjectiveForm();
				$this->controller->render('edit_3_objectives', array (
					'model' => $model
				));
			} else {
				$model = new ObjectiveForm();
				$this->controller->render('edit_1_objectives', array (
					'model' => $model
				));
			}
		}
	}

	protected function saveNewObjectiveForm($arr, $next) {
		$model = new NewObjectiveForm();
		$model->attributes = $arr;
		$model->save();

		if (!$model->save()) {
			$this->controller->render('edit_3_objectives', array (
				'model' => $model
			));
			return;
		} else {
			if ($next) {
				$model = Meetings::model()->findByPk($_GET['id']);
				$this->controller->render('edit_4_end_of_meeting', array (
					'model' => $model
				));
			} else {
				$model = new TopicValidationForm();
				$this->controller->render('edit_2_topics', array (
					'model' => $model
				));
			}
		}
	}

	protected function saveEndOfMeeting($arr, $next){
		if (isset($arr['next_date']))
		{
			if ($arr['next_date'] != "")
				$arr['next_date'] = Constants::toSqlDate($arr['next_date']);
			else
				unset($arr['next_date']);
		}
		$model = Meetings::model()->findByPk($_GET['id']);
		$model->setAttributes($arr, false);
		if (!isset($arr['next_date']))
			$model->next_date = null;
		else
			$model->next_date = $arr['next_date'];
		
		$model->completed = true;
		if (!$model->save()) {
			$model->completed = false;
			$this->controller->render('edit_4_end_of_meeting', array (
				'model' => $model
			));
			return;
		} else {
			if ($next) {
				$model = Meetings::model()->findByPk($_GET['id']);
				Yii::app()->user->setFlash('meeting', Yii::t('project', 'Meeting\'s informations have been saved.'));
				$this->controller->render('edit_4_end_of_meeting', array (
					'model' => $model
				));
			} else {
				$model->completed = false;
				$model = new NewObjectiveForm();
				$this->controller->render('edit_3_objectives', array (
					'model' => $model
				));
			}
		}

	}

}
?>
